package com.link.base.base.appduty.service;

import com.link.base.base.appduty.dao.mybatis.mapper.AppMenuMapper;
import com.link.base.base.appduty.model.*;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import javax.annotation.Resource;

import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>app端菜单</p>
 * <p>Description</p>
 * <p>Company</p>
 *
 * @author yrf
 * @date 2016年8月4日  下午4:09:03
 */

@Service
public class AppMenuServiceImpl extends BasicServiceImpl<AppMenu> implements AppMenuService {

    @Resource
    private AppMenuMapper appMenuMapper;

    @Resource
    private AppSecurityMenuService appSecurityMenuService;

    @Resource
    private AppMenuSecInterService appMenuSecInterService;

    @Resource
    private AppDutyMenuInterService appDutyMenuInterService;

    @Override
    public BasicMapper<AppMenu> getBasicMapper() throws Exception {
        if (appMenuMapper != null) {
            return appMenuMapper;
        }
        throw new ServiceException("PUBLIC-006");
    }

    @Override
    public List<AppMenu> queryUnSelectMenuListPage(AppMenu qps) {

        return appMenuMapper.queryUnSelectMenuListPage(qps);
    }

    @Override
    public Map<String, Object> getAllMenuList(AppMenu menu) throws Exception {
        Map<String, Object> result = new HashMap<>(8);
        // 获取当前职责已分配的菜单数据
        menu.setPageFlag(false);
        List<AppMenu> selectMenuList = appMenuMapper.querySelectMenuListPage(menu);
        if (selectMenuList.size() > 0) {
            for (AppMenu item: selectMenuList) {
                // 通过menuid，职责id获取对应的菜单-职责中间表记录
                AppDutyMenuInter appDutyMenuInter = new AppDutyMenuInter();
                appDutyMenuInter.setMenuId(item.getId());
                appDutyMenuInter.setDutyId(Long.parseLong(menu.getAttr1()));
                AppDutyMenuInter tempAppDutyMenu = appDutyMenuInterService.queryByExamplePage(appDutyMenuInter).get(0);
                if (null != tempAppDutyMenu) {
                    // 获取已选择的安全性菜单，安全性菜单记录的parentId为菜单-职责中间表的id
                    AppMenuSecInter secMenuInter = new AppMenuSecInter();
                    secMenuInter.setPageFlag(false);
                    secMenuInter.setParentId(tempAppDutyMenu.getId());
                    List<AppMenuSecInter> secMenuList = appMenuSecInterService.queryByExamplePage(secMenuInter);
                    List<AppSecurityMenu> list = new ArrayList<>();
                    if (secMenuList.size() > 0) {
                        for (AppMenuSecInter appMenuSecInter : secMenuList) {
                            AppSecurityMenu secMenu = new AppSecurityMenu();
                            secMenu.setId(appMenuSecInter.getSecurityId());
                            secMenu.setName(appMenuSecInter.getSecurity());
                            secMenu.setParentId(appMenuSecInter.getParentMenuId());
                            secMenu.setParentMenuName(appMenuSecInter.getParentMenuName());
                            secMenu.setParentMenuDutyId(appMenuSecInter.getParentId());
                            list.add(secMenu);
                        }
                    }
                    item.setSecMenuList(list);
                }
            }
        }
        // 获取所有菜单数据
        AppMenu record = new AppMenu();
        record.setPageFlag(false);
        record.setModuleType("menu");
        // 包含一级菜单和二级菜单的列表
        List<AppQuickSetVO> menuList = appMenuMapper.queryParentAndSubMenuList(record);
        if (menuList.size() > 0) {
            Map<String, Integer> showCountMap = new HashMap<>(4);
            for (AppQuickSetVO item: menuList) {
                // 通过showFlag控制前端table组件是否合并
                String recordKey = item.getParentMenuId().toString();
                if (showCountMap.containsKey(recordKey)) {
                    Integer num = showCountMap.get(recordKey);
                    num++;
                    showCountMap.replace(recordKey, num);
                } else {
                    showCountMap.put(recordKey, 1);
                    item.setShowFlag("Y");
                }
            }
            for (AppQuickSetVO item : menuList) {
                // 设置前端table组件合并的行数
                if ("Y".equals(item.getShowFlag())) {
                    Integer total = showCountMap.get(item.getParentMenuId().toString());
                    item.setShowNum(total);
                }
                // 获取安全性菜单
                long menuId = null != item.getSubMenuId() ? item.getSubMenuId() : item.getParentMenuId();
                AppSecurityMenu secMenu = new AppSecurityMenu();
                secMenu.setPageFlag(false);
                secMenu.setParentId(menuId);
                List<AppSecurityMenu> secMenuList = appSecurityMenuService.queryByExamplePage(secMenu);
                item.setSecMenuList(secMenuList);
            }
        }
        result.put("all", menuList);
        result.put("selected", selectMenuList);
        return result;
    }

    @Override
    public List<AppMenu> queryUserMenus(Long appRole, Long corpId) {
        if (corpId == null) {
            throw new BasicServiceException("请输入App角色ID");
        }
        return appMenuMapper.queryUserMenus(appRole, corpId);
    }
}
